requireJS(['constant', 'site', 'handlebars', 'public'], function (Constant, site, Handlebars, Public) {
    var url = {
        selectSenduserMan: "manager/selectSenduserMan.do",
        massSmsSend: "manager/massSmsSend.do"
    }

    var pageNum = 1;
    var users = [];
    var usersName = [];

    function querySendUser() {
        //users = [];
        //usersName = [];
        var frontData = {
            pageNum: pageNum
        }
        if ($("#type option:selected").val()) {
            frontData.select_type = $("#type option:selected").val()
        }
        /*if ($("#seleNames option:selected").val()) {
            frontData.type = $("#seleNames option:selected").val()
        }*/
        if ($("#inputNames").val()) {
            frontData.keyword = $("#inputNames").val();
        }
        site.send({
            url: url.selectSenduserMan,
            data: frontData,
            callback: function (data) {
                data = JSON.parse(data)
                if (data.type == 1) {
                    $('#person_list').remove();
                    $('#admin_list').remove();
                    if (frontData.select_type === '2' || !frontData.select_type) {
                        var source2 = $("#person_list_tpl").html();
                        var template2 = Handlebars.compile(source2);
                        var list2 = data.content.list;
                        var html2 = template2(list2);
                        $('.centerBox').append(html2);
                    } else {
                        var source = $("#admin_list_tpl").html();
                        var template = Handlebars.compile(source);
                        var list = data.content.list;
                        var html = template(list);
                        $('.centerBox').append(html);
                    }

                    $.each(users, function (i, e) {
                        $.each($("input[name='sendUserName']"), function (ii, ee) {
                            if (e === $(ee).val()) {
                                $(ee).prop('checked', true)
                            }
                        })
                    })


                    $("input[name='sendUserName']").on('click', function () {
                        var _this = this
                        if ($(_this).is(':checked')) {
                            users.push($(_this).val())
                            usersName.push($(_this).data('username'))
                        } else {
                            $.each(users, function (i, e) {
                                if ($(_this).val() === e) {
                                    users.splice(i, 1);
                                }

                            })
                            $.each(usersName, function (i, e) {
                                if ($(_this).data('username') === e) {
                                    usersName.splice(i, 1);
                                }
                            })
                        }
                        console.log(users);
                        console.log(usersName);
                    })

                    $(".person_winBox").show();

                    $('.person-page-container').createPager({
                        current: frontData.pageNum,
                        total: data.content.total,
                        pageSize: data.content.pageSize,
                        needInput: false,
                        backFn: function (current) {
                            pageNum = current;
                            querySendUser();
                        }
                    })

                }
            }
        })
    }

    $("#type").on('change', function () {
        //users = [];
        //usersName = [];
        pageNum = 1;
    })

    $("#selectSenduserMan").on('click', function () {
        querySendUser();
    })


    //搜索功能
    $(".showPerson").on('click', function () {
        pageNum = 1;
        querySendUser();
    })

    $('.confirm').on('click', function () {
        $('.person_winBox').toggle();
        if (usersName.length) {
            $('.choosedSendMan').html('');
            $.each(usersName, function (i, e) {
                $('.choosedSendMan').append('<span class="choose-per" data-username="' + e + '" data-user="' + users[i] + '"><span class="choose-perName">' + e + '</span><span class="removeRole" style="cursor: pointer;">×</span ></span>')
            })
            $(".removeRole").on('click', function () {
                var _this = this;
                console.log($(_this).parents('.choose-per').data('user'));
                $.each(users, function (i, e) {
                    if (e == $(_this).parents('.choose-per').data('user')) {
                        users.splice(i, 1)
                    }
                })
                $.each(usersName, function (i, e) {
                    if (e == $(_this).parents('.choose-per').data('username')) {
                        usersName.splice(i, 1)
                    }
                })
                $(_this).parents('.choose-per').remove();
                console.log(users);
                console.log(usersName);
                if (!$('.choose-per').length) {
                    $('.choosedSendMan').html("最多选择20个");
                }
            })
        } else {
            $('.choosedSendMan').html('最多选择20个');
        }
    })

    $('.sureSend').on('click', function () {
        if (!users.length) {
            Constant.fn.Message("I", "请选择短信接收人")
            return;
        }
        var content = $("#message").val();
        if (!content) {
            Constant.fn.Message("I", "请输入短信内容")
            return;
        }
        var dataUsers = "";
        $.each(users, function (i, e) {
            dataUsers += e + ','
        })
        dataUsers = dataUsers.substring(0, dataUsers.length - 1);
        site.send({
            url: url.massSmsSend,
            data: {
                phones: dataUsers,
                content: content
            }, callback: function (data) {
                data = JSON.parse(data);
                if (data.type === 1) {
                    Constant.fn.Message("T", data.msg);
                    $('.choosedSendMan').html("最多选择20个");
                    $("#message").val("");
                    users = [];
                    usersName = [];
                } else {
                    Constant.fn.Message("F", data.msg);
                }
            }
        })
    })
})